package com.atguigu.meinian.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.meinian.mapper.OrdersettingMapper;
import com.atguigu.meinian.pojo.OrderSetting;
import com.atguigu.meinian.service.OrdersettingService;
import com.atguigu.meinian.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zbystart
 * @create 2021-06-23 9:00
 */
@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Override
    public void saveExcelToDB(List<String[]> list) {

        List<Date> dates = new ArrayList<>();
        // TODO 在保存时应该判断数据类型是否合法？
        List<OrderSetting> collect = list.stream().map(item -> {
            OrderSetting orderSetting = null;
            try {
                // 2021/2/17
                Date date = new SimpleDateFormat("yyyy/M/dd").parse(item[0]);
                Integer number = Integer.valueOf(item[1]);
                orderSetting = new OrderSetting();
                // 收集预约数据
                dates.add(date);
                orderSetting.setOrderDate(date);
                orderSetting.setNumber(number);
            } catch (Exception e) {

                // TODO 代码走到这的话可能，表格中的数据可能是不和法的
                e.printStackTrace();
            }
            return orderSetting;
        })
        .filter(item -> item != null)
        .collect(Collectors.toList());
        // 在保存之前先删除数据库之前的日期预约信息
        ordersettingMapper.deleteOrdersettingByDate(dates);
        ordersettingMapper.insertOrdersetting(collect);
    }

    @Override
    public List<Map<String, Integer>> getOrdersettingByMonth(String date) {
        // 这一个月的起始时间和结束时间
        String beginStr = date + "-1";
//        String endStr = date + "-31";
        String endStr = DateUtils.getLastDayOfMonth(date);


        // 格式化起始时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String, Integer>> data = null;
        try {
            Date beginDate = simpleDateFormat.parse(beginStr);
            Date endDate = simpleDateFormat.parse(endStr);
            // 查这一个月所有预约数据
            List<OrderSetting> list = ordersettingMapper.selectOrdersettingByMonth(beginDate,endDate);
            // 封装数据
            // { date: 1, number: 120, reservations: 1 },
            data = list.stream().map(item -> {
                Map<String, Integer> map = new HashMap<>();
                map.put("date", item.getOrderDate().getDate());
                map.put("number", item.getNumber());
                map.put("reservations", item.getReservations());
                return map;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    @Override
    public void updateNumberByDate(String orderDate,Integer number) {
        // 先判断当前日期是否有预约数据
        OrderSetting date = ordersettingMapper.selectOrdersettingByDate(orderDate);
        if (date != null) {
            // 有预约数据
            ordersettingMapper.updateNumberByDate(orderDate,number);
        } else {
            // 没有
            ArrayList<OrderSetting> orderSettings = new ArrayList<>();
            try {
                Date pase = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
                orderSettings.add(new OrderSetting(pase,number));
                ordersettingMapper.insertOrdersetting(orderSettings);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void deletePrevYearOrdersettingInfo() {
        // 获取当前的年份
        Calendar calendar= Calendar.getInstance();
        // 获取上一年
        int prevYear = calendar.getWeekYear() - 1;
        ordersettingMapper.deletePrevYearOrdersettingInfo(prevYear);
    }
}
